package com.xca.is.utils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


public class ReplaceFragmentUtils {

    private final FragmentManager fragmentManager;

    public ReplaceFragmentUtils(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;

    }

    /**
     * 替换当前Fragment为新的Fragment。
     *
     * @param newFragment 要添加的新Fragment实例。
     * @param containerId 容器的ID，通常是Activity中的FrameLayout。
     */
    public void replaceFragment(Fragment newFragment, int containerId) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.replace(containerId, newFragment);
        transaction.commitAllowingStateLoss(); // 提交事务, 允许在非UI线程中提交事务
    }
}
